<?php

namespace app\shop\controller;

use \think\Controller;
use \think\Db;

class Manage extends Base
{


    /**
     * 商品管理：goodManage
     *
     * @return array 返回数组
     */
    public function goodManage()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $type = Db::table('up_shop_type')
            ->where('st_shopid', $user_id)
            ->order('st_order')
            ->select();
//        //-----------给上默认的分类-------------
//        if(empty($type)){
//            $admintypes = Db::table('up_shop_type')
//                ->where('st_status', 2)
//                ->where('st_shopid', 0)
//                ->select();
//            foreach ($admintypes as $v){
//                unset($v['st_id']);
//                $v['st_shopid'] = $user_id;
//                Db::table('up_shop_type')->insert($v);
//            }
//        }
        //------------给上默认的分类end-----------
        if ($type) {
            if (!isset($typeid)) {
                $typeid = $type[0]['st_id'];
            }
            $good = Db::table('up_goods')
                ->where('g_shop', $user_id)
                ->where('g_type', $typeid)
                ->where('g_status', '<',3)
                ->order('g_time DESC')
                ->page($page, 10)
                ->select();
            if($good){
                foreach ($good as $k=>$v){//计算月销量
                    $xiao_l = Db::table('up_order_info')
                        ->where('oi_good_id',$v['g_id'])
                        ->whereTime('oi_pay_time','month')
                        ->field('SUM(oi_num) as monthNum')
                        ->find();
                    $good[$k]['monthNum'] = $xiao_l['monthNum'];
                }
            }
            //------------------------------------------------
            return json_encode(['status' => 200,'msg'=>$good]);
        }else{
            return json_encode(['status' => 200, 'msg' => array()]);
        }

    }
    /**
     * 获取我的所有商品
     * @return array 返回数组
     */
    public function getMyGoods()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $good = Db::table('up_goods')
            ->where('g_shop', $user_id)
            ->where('g_status', 1)
            ->order('g_time DESC')
            ->page($page, 10)
            ->field('g_name,g_thumb,g_id')
            ->select();
        return json_encode(['status' => 200,'msg'=>$good]);

    }

    /**
     * 新建商品:newGood
     */
    public function newGood()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['g_name'] = $gname;
        //敏感词
        $have = $this->fuckWord($gname);
        if($have == true){
            return json_encode(['status' => 0,'msg'=>'名字含有敏感词']);
        }
        $data['g_thumb'] = $thumb;
        $data['g_image'] = trim($image,',');
        $data['g_price_before'] = $before;
        if(!isset($now)){
            $now = $before;
        }
        $data['g_price_now'] = $now;
        $data['g_type'] = $type;
        $data['g_shop'] = $user_id;
        $data['g_pack'] = $pack;//包装费
        $data['g_weight'] = $weight;//包装费
        if(!isset($stock) || $stock <= 0){
            $stock = -1;
        }
        $data['g_stock'] = $stock;//kucun
        $data['g_canbuy'] = $canbuy;//kucun
        $data['g_time'] = date('Y-m-d H:i:s');//
        $res = Db::table('up_goods')->insert($data);
        if($res){
            return json_encode(['status' => 200,'msg'=>'ok']);
        }else{
            return json_encode(['status' => 0,'msg'=>'新增失败']);
        }
    }

    /**
     * 编辑商品:editGood
     */
    public function editGood()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['g_name'] = $gname;
        //敏感词
        $have = $this->fuckWord($gname);
        if($have == true){
            return json_encode(['status' => 0,'msg'=>'名字含有敏感词']);
        }
        $data['g_thumb'] = $thumb;
        $data['g_image'] = trim($image,',');
        $data['g_price_before'] = $before;
        if(!isset($now)){
            $now = $before;
        }
        $data['g_price_now'] = $now;
        $data['g_type'] = $type;
        $data['g_shop'] = $user_id;
        $data['g_pack'] = $pack;//包装费
        $data['g_weight'] = $weight;//包装费
        if(!isset($stock) || $stock <= 0){
            $stock = -1;
        }
        $data['g_stock'] = $stock;//kucun
        $data['g_canbuy'] = $canbuy;//kucun
//        $data['g_time'] = date('Y-m-d H:i:s');//
        $res = Db::table('up_goods')
            ->where('g_id',$g_id)->update($data);
        if($res){
            return json_encode(['status' => 200,'msg'=>'ok']);
        }else{
            return json_encode(['status' => 0,'msg'=>'编辑失败']);
        }
    }
    /**
     * 商品详情
     */
    public function goodInfo()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $res = Db::table('up_goods')->where('g_id',$g_id)->find();
        if($res['g_image']){
            $res['g_image'] = explode(',',$res['g_image']);
        }
        return json_encode(['status' => 200,'msg'=>$res]);
    }

    /**
     * 获取我的分类
     */
    public function getMyShopType()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $type = Db::table('up_shop_type')
            ->where('st_shopid', $user_id)
            ->order('st_order')
            ->select();
        //-----------给上默认的分类-------------
//        if(empty($type)){
//            $admintypes = Db::table('up_shop_type')
//                ->where('st_status', 2)
//                ->where('st_shopid', 0)
//                ->select();
//            foreach ($admintypes as $v){
//                unset($v['st_id']);
//                $v['st_shopid'] = $user_id;
//                Db::table('up_shop_type')->insert($v);
//            }
//        }
        //------------给上默认的分类end-----------
        return json_encode(['status' => 200,'msg'=>$type]);
    }
    /**
     * 我的分类的排序：shopTypeOrder
     * type,st_id
     */
    public function shopTypeOrder()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        //判断此节的order——start
        $order = Db::table('up_shop_type')->where('st_id',$st_id)->field('st_order,st_id')->find();
        //====================================
        if($type == 1){
            $zorder = Db::table('up_shop_type')
                ->where('st_shopid',$user_id)
                ->field('st_order,st_id')
                ->order('st_order')
                ->select();
            if($zorder[0]['st_order'] == $order['st_order']){
                return json_encode(['status'=>0,'msg'=>'已经是第一个了']);
            }
        }else{
            $zorder = Db::table('up_shop_type')
                ->where('st_shopid',$user_id)
                ->field('st_order,st_id')
                ->order('st_order DESC')
                ->select();
            if($zorder[0]['st_order'] == $order['st_order']){
                return json_encode(['status'=>0,'msg'=>'已经是最后一个了']);
            }
        }

        $uporder = 0;
        $upid = 0;
        foreach ($zorder as $k=>$v){
            if($v['st_order'] == $order['st_order']){
                $upid = $k-1;
            }
        }
        $uporder = $zorder[$upid]['st_order'];
        $meorder = $order['st_order'];
        $ores = Db::table('up_shop_type')
            ->where('st_id',$zorder[$upid]['st_id'])->update(['st_order'=>$meorder]);
        $tres = Db::table('up_shop_type')
            ->where('st_id',$st_id)->update(['st_order'=>$uporder]);
        //========================================================================
        if($ores && $tres){
            return json_encode(['status'=>200]);
        }else{
            return json_encode(['status'=>0]);
        }
    }
    /**
     * 新建分类:shopNewType
     *$name;$thumb
     */
    public function shopNewType()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $order = Db::table('up_shop_type')
            ->where('st_shopid',$user_id)
            ->field('st_order,st_id')
            ->order('st_order DESC')
            ->select();
        $data = array();
        $data['st_name'] = $name;
        $data['st_thumb'] = $thumb;
        $data['st_shopid'] = $user_id;
        if($order){
            $data['st_order'] = $order[0]['st_order']+1;
        }else{
            $data['st_order'] = 1;
        }
        $res = Db::table('up_shop_type')->insert($data);
        if($res){
            return json_encode(['status'=>200]);
        }else{
            return json_encode(['status'=>0]);
        }
    }
    /**
     * 图标
     *
     */
    public function tubiao()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_admin_tubiao')
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 编辑分类
     *$name;$thumb；st_id
     */
    public function editType()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['st_name'] = $name;
        $data['st_thumb'] = $thumb;
        $res = Db::table('up_shop_type')->where('st_id',$st_id)->update($data);
        if($res){
            return json_encode(['status'=>200]);
        }else{
            return json_encode(['status'=>0]);
        }
    }
    /**
     * 删除分类：deleteType
     * st_id
     */
    public function deleteType()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $res = Db::table('up_shop_type')->where('st_id',$st_id)->delete();
        if($res){
            return json_encode(['status'=>200]);
        }else{
            return json_encode(['status'=>0]);
        }
    }
    /**
     * 商品的批量操作:shopGoods
     * goodsid,type（1是上架 2是下架  3是删除）
     */
    public function shopGoods()
    {
        extract(input());
        $auth = $this->checkAll($user_id, $time, $value, $reqKey);
        if ($auth == false) {
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $gids = trim($goodsid,',');
        $res = Db::table('up_goods')->where('g_id','in',$gids)->update(['g_status'=>$type]);
        if($res){
            return json_encode(['status'=>200]);
        }else{
            return json_encode(['status'=>0]);
        }
    }

}